--[[
	GGG Postal 2 Heart thing

	By: Kogitsune
]]--

local Heart

Heart = { }

Heart.W 		= 64						--Width of the heart
Heart.H 		= 64						--Height of the heart
Heart.X 		= surface.ScreenWidth( ) 	- Heart.W	--X Pos
Heart.Y 		= surface.ScreenHeight( ) 	- Heart.H * 4	--Y Pos
Heart.Texture 		= surface.GetTextureID( "hart" )		--The texture to use to draw the heart
Heart.ForeColor 	= Color( 189, 000, 000, 255 )			--The color to color the hear with
Heart.TallChange 	= 00						--Current Height adjustment
Heart.WideChange 	= 00						--Current Width adjustment
Heart.MaxTallChange 	= .1						--Maximum height adjustment ( absolute - varies from -MAX to MAX )
Heart.MaxWideChange 	= .3						--Same as above, but for width
Heart.PulseTime		= .125						--How long a cycle for the pulse ( 0 to either MAX should be )
Heart.PulseScaleMin 	= 00						--Minumum pulse scale for bar and heart
Heart.PulseScaleMax 	= 10 						--Maximum for above
Heart.AdvanceLeft 	= false						--Determines if movement should be left or right
Heart.AdvanceUp 	= false						--Determines if movement should be up or down
Heart.TargetPulse	= 0						--What the bar's pulse should be
Heart.CurrentPulse	= 0						--What the bar's pulse is

function Heart:Paint( )
	local wchange, hchange, pl, w, h, frac, delta, ft, pulse

	pl = LocalPlayer( )
	ft = FrameTime( )
	pulse = math.min( pl:GetNWFloat( "Pulse", self.PulseScaleMin ), self.PulseScaleMax )

	frac = pulse / self.PulseScaleMax

	wchange = ft / self.PulseTime * frac * self.MaxWideChange
	hchange = ft / self.PulseTime * frac * self.MaxTallChange

	self.TargetPulse = pulse

	if self.CurrentPulse < self.TargetPulse then
		self.CurrentPulse =  math.Clamp( self.CurrentPulse + ft * self.PulseScaleMax, self.PulseScaleMin, self.TargetPulse  )
	else
		self.CurrentPulse =  math.Clamp( self.CurrentPulse - ft * self.PulseScaleMax, self.TargetPulse  , self.CurrentPulse )
	end

	if self.AdvanceLeft then
		self.WideChange = math.max( -self.MaxWideChange, self.WideChange - wchange )
	else
		self.WideChange = math.min(  self.MaxWideChange, self.WideChange + wchange )
	end

	if self.AdvanceUp then
		self.TallChange = math.max( -self.MaxTallChange, self.TallChange - hchange )
	else
		self.TallChange = math.max(  self.MaxTallChange, self.TallChange - hchange )
	end

	if self.WideChange == -self.MaxWideChange then	self.AdvanceLeft 	= false		end
	if self.TallChange == -self.MaxTallChange then	self.AdvanceUp 		= false		end
	if self.WideChange ==  self.MaxWideChange then	self.AdvanceLeft 	= true		end
	if self.TallChange ==  self.MaxTallChange then	self.AdvanceUp 		= true		end

	surface.SetTexture( self.Texture )
	surface.SetDrawColor( self.ForeColor.r, self.ForeColor.g, self.ForeColor.b, self.ForeColor.a )

	w = self.W * ( 1 + self.WideChange )
	h = self.H * ( 1 + self.TallChange )

	surface.DrawTexturedRect( self.X - w / 2, self.Y - h / 2, w, h )

	surface.SetDrawColor( 189, 189, 189, 255 )
	surface.DrawOutlinedRect( self.X - self.W / 2, self.Y + self.H, self.W, self.H * .25 )

	frac = self.CurrentPulse / self.PulseScaleMax

	surface.SetDrawColor( 189 * frac, 189 * ( 1 - frac ), 000, 255 )
	surface.DrawRect( self.X - self.W / 2 + 1, self.Y + self.H + 1, self.W * frac - 2, self.H * .25 - 2 )
	
end

hook.Add( "HUDPaint", "GGG:Heart", function( ) Heart:Paint( ) end )
